﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;
using Model;
using Bll;

namespace 财会系
{
    public partial class EmailTableTwo : System.Web.UI.Page
    {
        BllEmail b = new BllEmail();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string number = num();

                Hidden1.Value = number;
                txtCodeShow.Text = number;
            }
        }

        public static string num()
        {
            string[] rander = { "p", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "s", "d", "f", "g", "h", "j", "k", "l", "q", "w", "e", "r", "t", "y", "u", "i", "o", "z", "x", "v", "c", "b", "n", "m", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M" };
            int sub;
            string yzm = "";
            Random rand = new Random();
            rand = new Random(345 * unchecked((int)DateTime.Now.Second));
            for (int i = 0; i < 4; i++)
            {

                sub = rand.Next(61);
                yzm += rander[sub];

            }
            return yzm;

        }

        public void clear()
        {
            txtAdress.Text = "";
            txtCode.Text = "";
            txtEmail.Text = "";
            txtName.Text = "";
            txtPhone.Text = "";
            txtQQ.Text = "";
            txtTitle.Text = "";
            txtWord.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            clear();
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            bool tfName, tfPhone11, tfPhone8, tfEmail;

            //判断是否为中文
            Regex RgName = new Regex("[\u4e00-\u9fa5]");
            Match mName = RgName.Match(txtName.Text.Trim());
            tfName = mName.Success;

            //判断是否为11位手机号码
            Regex RgPhone11 = new Regex("[0-9]{11,11}");
            Match mPhone11 = RgPhone11.Match(txtPhone.Text.Trim());
            tfPhone11 = mPhone11.Success;

            //判断是否为8位手机号码
            Regex RgPhone8 = new Regex("[0-9]{8,8}");
            Match mPhone8 = RgPhone8.Match(txtPhone.Text.Trim());
            tfPhone8 = mPhone8.Success;

            //判断是否为邮箱
            Regex RgEmail = new Regex(@"([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,5})+");
            Match mEmail = RgEmail.Match(txtEmail.Text.Trim());
            tfEmail = mEmail.Success;

            if (txtName.Text.Trim() != null)
            {
                if (tfName)
                {
                    if (txtPhone.Text.Trim() != null)
                    {
                        if (tfPhone11 || tfPhone8)
                        {
                            if (txtEmail.Text.Trim() != null)
                            {
                                if (tfEmail)
                                {
                                    if (txtTitle.Text.Trim() != null)
                                    {
                                        if (txtWord.Text.Trim() != null)
                                        {
                                            string code = Hidden1.Value;
                                            if (txtCode.Text.Trim().ToLower() == code.ToLower())
                                            {
                                                ModelPublic m = new ModelPublic();
                                                m.Name ="'"+ txtName.Text.Trim() + "'";
                                                if (txtQQ.Text.Trim() == "")
                                                    m.QQ = "NULL";
                                                else
                                                    m.QQ = "'" + txtQQ.Text.Trim() + "'";
                                                if (txtAdress.Text.Trim() == "")
                                                    m.Adress = "NULL";
                                                else
                                                    m.Adress = "'" + txtAdress.Text.Trim() + "'";
                                                m.Phone = "'" + txtPhone.Text.Trim() + "'";
                                                m.Email = "'" + txtEmail.Text.Trim() + "'";
                                                m.Title = "'" + txtTitle.Text.Trim() + "'";
                                                m.Content = "'" + txtWord.Text.Trim()+"'";

                                                Response.Write("<script>alert('" + b.Email_Secretary_Add(m) + "')</script>");
                                                clear();
                                            }
                                            else
                                            {
                                                Response.Write("<script>alert('验证码不正确！')</script>");
                                            }
                                        }
                                        else
                                        {
                                            Response.Write("<script>alert('留言内容不能为空！')</script>");
                                        }
                                    }
                                    else
                                    {
                                        Response.Write("<script>alert('留言主题不能为空！')</script>");
                                    }
                                }
                                else
                                {
                                    Response.Write("<script>alert('请正确填写邮箱地址！')</script>");
                                }
                            }
                            else
                            {
                                Response.Write("<script>alert('联系邮箱不能为空！')</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('请正确填写11位或8位联系电话！')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('联系电话不能为空！')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('请正确填写中文姓名！')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('姓名不能为空！')</script>");
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string number = num();

            Hidden1.Value = number;
            txtCodeShow.Text = number;
        }

    }
}